package com.google.code.jbauble.mvc.render;

import java.io.IOException;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.jbauble.mvc.Result;
import com.google.code.jbauble.mvc.ViewRender;
import com.google.code.jbauble.mvc.support.UrlHelper;

public class RedirectViewRender implements ViewRender {
	@Override
	public void render(HttpServletRequest request,
			HttpServletResponse response, Result result, Map exts) {
		try {
			String url = UrlHelper.mergeUrl(request.getContextPath(), result.getUrl());
			if (result.getType() == Result.REDIRECT) {
				response.sendRedirect(url);
			}
			else {
				RequestDispatcher dispatcher = request.getRequestDispatcher(url);
				dispatcher.forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
